package com.example.citydev;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
	private static final String NAMESPACE = "http://WSpkg/";
	private static String MYIP = "10.0.0.11";
	private static String URL = "http://" + MYIP
			+ ":8080/CityDevApp-war/CityDevWS?WSDL";
	private static final String METHOD_NAME = "login";
	private static final String SOAP_ACTION = NAMESPACE + METHOD_NAME;
	private static String risultatoWS = "";
	private static String currentUSERNAME = "";
	private static String currentPASSWORD = "";
	TextView tvLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		tvLogin = (TextView) findViewById(R.id.loginTV);
		Button button = (Button) findViewById(R.id.button_login);
		button.setOnClickListener(this);
		button = (Button) findViewById(R.id.button_anonimo);
		button.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		tvLogin.setText("");
		final EditText user = (EditText) findViewById(R.id.user);
		final EditText pass = (EditText) findViewById(R.id.pass);
		currentUSERNAME = user.getText().toString();
		currentPASSWORD = pass.getText().toString();

		switch (v.getId()) {
		case R.id.button_login:
			AsyncCallWS task = new AsyncCallWS();
			task.execute();
			break;
		case R.id.button_anonimo:
			Bundle bundle = new Bundle();
			bundle.putString("user", "anonimo");
			Intent form_intent = new Intent(getApplicationContext(),Welcome.class);
			form_intent.putExtras(bundle);
			startActivity(form_intent);
			break;
		}

	}

	public static String invokeWSMethod() {
		String resTxt = null;
		// Create request
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		// Property which holds input parameters
		PropertyInfo prop1 = new PropertyInfo();
		// Set Name
		prop1.setName("username");
		// Set Value
		prop1.setValue(currentUSERNAME);
		// Set dataType
		prop1.setType(String.class);
		// Add the property to request object
		request.addProperty(prop1);
		// Create envelope
		PropertyInfo prop2 = new PropertyInfo();
		// Set Name
		prop2.setName("password");
		// Set Value
		prop2.setValue(currentPASSWORD);
		// Set dataType
		prop2.setType(String.class);
		// Add the property to request object
		request.addProperty(prop2);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		// Set output SOAP object
		envelope.setOutputSoapObject(request);
		// Create HTTP call object
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

		try {
			// Invoke web service
			androidHttpTransport.call(SOAP_ACTION + METHOD_NAME, envelope);
			// Get the response
			SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
			// Assign it to resTxt variable static variable
			risultatoWS = response.toString();

		} catch (Exception e) {
			// Print error
			e.printStackTrace();
			// Assign error message to resTxt
			resTxt = "Error occured";
		}
		// Return resTxt to calling object
		return resTxt;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	private class AsyncCallWS extends AsyncTask<String, Void, Void> {
		@Override
		protected Void doInBackground(String... params) {
			MainActivity.invokeWSMethod();
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// Set response
			tvLogin.setText("");
			if (risultatoWS.equals("true")) {
				Bundle bundle = new Bundle();
				bundle.putString("user", currentUSERNAME);

				Intent form_intent = new Intent(getApplicationContext(),
						Welcome.class);
				form_intent.putExtras(bundle);
				startActivity(form_intent);

			} else {
				tvLogin.setText("Username o Password non valida");
			}

		}

		@Override
		protected void onPreExecute() {
			// Make ProgressBar invisible
			tvLogin.setText("Sto contattando il server login..");
		}

		@Override
		protected void onProgressUpdate(Void... values) {
		}

	}
}
